<template>
	<view class="container">
		<view class="search-wrapper">
			<view class="search-inner">
				<uni-icons 
					type="search"
					class="search-icons"
					color="#3D3D3D"
					size="24">
				</uni-icons>
				<input  
					type="text" 
					class="search-inp"
					placeholder="搜索证书名称"
					confirm-type="search"
					v-model="keyword"
					@confirm="handleSearch"
				/>
			</view>
		</view>
		<view class="menu-wrapper">
			<view class="menu-left">已选</view>
			<scroll-view
				class="scroll-view_H" 
				scroll-x="true" 
				:scroll-with-animation="true"
				:scroll-left="scrollLeft"
			>	
				<view 
					v-for="(item,index) in activeArgs" 
					:key="item.id"
					class="scroll-view-item_box"
				>
					<view 
						class="scroll-view-item_H"
					>
						<view class="scroll-item-text">
							{{item.title}}
						</view>
						<uni-icons
							type="closeempty"
							class="scroll-icons"
							color="#005AFF"
							@click="handleDelItem(index)"
							size="16">
						</uni-icons>
					</view>
				</view>
			</scroll-view>
		</view>
		
		<view class="content-wrapper">
			<view class="content-left">
				<scroll-view scroll-y="true" class="scroll-Y">
					<view 
						:class="item.id === cateId ? 'scroll-view-item active' : 'scroll-view-item'"
						v-for="(item,index) in categorizeArgs"
						:key="item.id"
						@click="handleChooseCate(item.id)"
					>
						{{item.name}} 
					</view>
				</scroll-view>
			</view>
			<view class="content-right">
				<scroll-view scroll-y="true" class="scroll-Y"> 
					<view
						:class="item.selected ? 'scroll-view-item active1' : 'scroll-view-item'"
						v-for="(item,index) in skillArgs"
						:key="item.id"
						@click="handleChooseSkill(index)"
					>
						{{item.title}} 
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="foot-wrapper">
			<view 
				class="foot-btn" 
				hover-class="click-btn"
				@click="handleToOther"
			>
				下一步
			</view>
		</view>
	</view>
</template>

<script>
	import { mapGetters, mapActions } from 'vuex'
	export default {
		data() {
			return {
				scrollLeft: 0,
				cateId: 1,
				skillId: 1,
				categorizeArgs: [],
				skillArgs: [],
				keyword: ''
			}
		},
		computed: {
			...mapGetters(
				[
				 "activeArgs"
				]
			)
		},
		onLoad () {
			this.fetchCategory()
		},
		methods: {
			...mapActions('certificate',['setActiveArgs']),
			handleDelItem (index) {
				let d = JSON.parse(JSON.stringify(this.activeArgs))
				let a = JSON.parse(JSON.stringify(this.skillArgs))
				d.splice(index,1)
				for (let obj of a) {
					const res = d.filter((item) => item.id === obj.id)
					if (res.length) {
						obj.selected = true
					} else {
						obj.selected = false
					}
				}
				this.skillArgs = a
				this.setActiveArgs(d)
			},
			handleFilter () {
				let d = JSON.parse(JSON.stringify(this.activeArgs))
				let a = JSON.parse(JSON.stringify(this.skillArgs))
				for (let obj of a) {
					const res = d.filter((item) => item.id === obj.id)
					if (res.length) {
						obj.selected = true
					} else {
						obj.selected = false
					}
				}
				this.skillArgs = a
			},
			handleChooseCate (id) {
				this.cateId = id
				this.fetchCertificateList(id)
			},
			handleSearch () {
				this.fetchCertificateList(this.cateId)
			},
			handleChooseSkill (index) {
				let d = JSON.parse(JSON.stringify(this.skillArgs))
				let a = JSON.parse(JSON.stringify(this.activeArgs))
				d[index].selected = !d[index].selected
				if (d[index].selected) {
					a.push(d[index])
					this.setActiveArgs(a)
				} else {
					let newA = a.filter((item) => item.id !== d[index].id )
					this.setActiveArgs(newA)
				}
				this.skillArgs = d	
			},
			handleToOther () {
				uni.navigateTo({
					url: "/pages/other/other"
				})
			},
			async fetchCategory () {
				try {
					const response = await this.$api.fetchCategory()
					this.categorizeArgs = response.data.reverse()
					if (this.categorizeArgs.length) {
						this.fetchCertificateList(this.categorizeArgs[0].id)
					}
					
				} catch (e) {
					console.log("pages: certificate (fetchCategory)error:->",e)
				}
			},
			async fetchCertificateList (id) {
				try {
					const response = await this.$api.fetchCertificateList({
						pid: id,
						keyword: this.keyword
					})
					let newArray = []
					for (let obj of response.data) {
						newArray.push({
							...obj,
							selected: false
						})
					}
					this.skillArgs = newArray
					this.handleFilter()
				} catch (e) {
					console.log("pages: certificate (fetchCertificateList)error:->",e)
				}
			}
			
		}
	}
</script>

<style lang="scss">
page {
	height: 100%;
	width: 100%;
}
.container {
	height: 100%;
	width: 100%;
	display: flex;
	flex-direction: column;
	.search-wrapper {
		padding: 20rpx 60rpx 0 60rpx;
		.search-inner {
			background: #EDEDED;
			border-radius: 8rpx;
			height: 70rpx;
			display: flex;
			align-items: center;
			.search-icons {
				margin-left: 20rpx;
			}
			.search-inp {
				flex: 1;
				height: 70rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				font-size: 28rpx;
				color: #666;
			}
		}
	}
	.menu-wrapper {
		width: 100%;
		height: 70rpx;
		padding: 0rpx 60rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		.menu-left {
			font-size: 24rpx;
			color: #AFAFAF;
			padding-right: 20rpx;
			white-space: nowrap;
		}
		.scroll-view_H {
			white-space: nowrap;
			flex: 1;
			overflow: auto;
			.scroll-view-item_box {
				display: inline-block;
				margin-right: 20rpx;
				.scroll-view-item_H {
					width: 180rpx;
					height: 50rpx;
					background: #A8C7FF;
					border-radius: 3rpx;
					border: 1rpx solid #2756AC;
					padding: 10rpx;
					box-sizing: border-box;
					display: flex;
					align-items: center;
					.scroll-item-text {
						font-size: 20rpx;
						color: #005AFF;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
				
					}
				}
			}
			
		}
	}
	.content-wrapper {
		flex: 1;
		padding-top: 14rpx;
		box-sizing: border-box;
		height: 100%;
		display: flex;
		justify-content: space-between;
		overflow: hidden;
		.content-left {
			height: 100%;
			width: 240rpx;
			background: #D9D9D9;
			box-sizing: border-box;
			overflow: hidden;
			
			.scroll-Y {
				height: 100%;
				width: 240rpx;
				overflow: hidden;
				box-sizing: border-box;
				.scroll-view-item {
					line-height: 100rpx;
					width: 250rpx;
					font-size: 30rpx;
					color: #000;
					padding: 0 20rpx;
					box-sizing: border-box;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
				.active {
					background: #fff;
				}
			}
		}
		.content-right {
			height: 100%;
			box-sizing: border-box;
			overflow: hidden;
			padding-right: 60rpx;
			.scroll-Y {
				height: 100%;
				overflow: hidden;
				box-sizing: border-box;
				.scroll-view-item {
					line-height: 80rpx;
					width: 400rpx;
					background: #E9E9E9;
					text-align: center;
					margin-top: 20rpx;
					border: 1rpx solid #E9E9E9;
				}
				.active1 {
					background: #A8C7FF;
					color: #2756AC;
					border: 1rpx solid #2756AC;
				}
			}
		}
	}
	.foot-wrapper {
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;
		.foot-btn {
			line-height: 70rpx;
			width: 450rpx;
			background: #005AFF;
			border-radius: 5rpx;
			font-size: 30rpx;
			color: #FFFFFF;
			text-align: center;
		}
		.click-btn {
			background: #4481f1;
		}
	}
}
</style>
